package day04.demo

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author wsl
 * @Description
 * 热门商品topN  点击次数、下单次数、支付次数
 * 优化
 * 提前补0，减少reduceByKey的次数
 *
 */
object TopN_Hot3 {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("rdd").setMaster("local[*]")
    val sc = new SparkContext(conf)

    val rdd: RDD[String] = sc.textFile("sparkcore/input/user_visit_action.txt")

     rdd.flatMap(
      action => {
        val datas: Array[String] = action.split("_")

        if (datas(6) != "-1") {
          //点击数据
          List((datas(6), (1, 0, 0)))
        } else if (datas(8) != "null") {

          //下单数据
          val arr: Array[String] = datas(8).split(",")
          arr.map((_, (0, 1, 0)))

        } else if (datas(10) != "null") {
          //支付数据
          val arr: Array[String] = datas(10).split(",")
          arr.map((_, (0, 0, 1)))
        } else {
          Nil
        }
      }
    )
       .reduceByKey(
         (t1, t2) => (t1._1 + t2._1, t1._2 + t2._2, t1._3 + t2._3)
       )
       .collect().foreach(println)

    sc.stop()

  }
}
